angular.module('CheckWork')

.factory('CheckWorkHomeService', ['$q', 'Http',
    function($q, Http) {
        // //获取作业检查的单品数据
        // function getQCDefaultInfoData(code,workID) {
        //     var defer = $q.defer();
        //     Http.post({
        //         url: "/api/FirstQC/getFirstQCDefaultInfo",
        //         data: {
        //             Parameter: {
        //                 Barcode: code,
        //                 WorkID: workID
        //             }
        //         }
        //     }).then(function(data) {
        //         defer.resolve(data);
        //     }, function(err) {
        //         defer.reject(err);
        //     });
            
        //     return defer.promise;
        // }
        // //获取当前设备的作业信息 00:11:22:33
        // function getTerminalWork(TerminalUniqueID) {

        //     var deferred = $q.defer();

        //     Http.post({
        //         url: "/api/FirstQC/getTerminalWorkLocationWorks",
        //         data: {
        //             Parameter: {
        //                 TerminalUniqueID: TerminalUniqueID, // 设备标识
        //             }
        //         }
        //     }).then(function(data) {
        //         deferred.resolve(data);
        //     }, function(err) {
        //         deferred.reject(err);
        //     });

        //     return deferred.promise;
        // }
        //获取缺陷信息列表
        function getBadTypeList(ItemMasterId,WorkId) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/FirstQC/getBadType",
                data: {
                    Parameter: {
                        ItemMasterId: ItemMasterId, 
                        WorkId: WorkId
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        //获取扩展枚举（位置缺陷信息列表）
        function getBadPositionList(code) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/Judge/getExtendEnum",
                data: {
                    Parameter: {
                        Code: code,   
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        //提交检查作业的信息
        function submitBadData(barcode,barcodeId,itemMasterId,deviceId,deviceName,workId,workName,WorkLocationId,WorkLocationName,userId,QCStatus,badRecords) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/CheckWork/submitCheckWork",
                data: {
                    Parameter: {
                        Barcode: barcode,  
                        BarcodeMaster: barcodeId, 
                        ItemMaster: itemMasterId,
                        Terminal: deviceId,
                        TerminalName: deviceName,
                        Work: workId,
                        WorkName: workName,
                        WorkLocation: WorkLocationId,
                        WorkLocationName: WorkLocationName,
                        UserID: userId,
                        QCStatus: QCStatus,
                        badRecords: badRecords
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        //获取barcode信息
        function getBarcodeData(Barcode) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/CheckWork/getBarcodeData",
                data: {
                    Parameter: {
                        Barcode: Barcode,    
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        //获取待修改的barcode缺陷信息
        //根据“芯片ID+当前作业”到“条码流水”中获取最后一笔记录。如获取到则进入修改模式，否则为新增模式。
        function getBarcodeDefectData(barcodeId,workId) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/CheckWork/getBarcodeDefectData",
                data: {
                    Parameter: {
                        BarcodeId: barcodeId,
                        Work:workId    
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        //更新全检数据
        function updateBadData(bizRecordId,barcodeId,barcode,userId,badRecords) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/CheckWork/updateBadData",
                data: {
                    Parameter: {
                        Barcode:barcode,
                        BarcodeId: barcodeId,
                        User:userId,
                        BizRecord:bizRecordId,
                        badRecords:badRecords    
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        

        return {
            
            getBadTypeList: getBadTypeList,
            getBadPositionList : getBadPositionList,
            submitBadData : submitBadData,
            getBarcodeData : getBarcodeData,
            getBarcodeDefectData : getBarcodeDefectData,
            updateBadData : updateBadData


        };

    }
]);